#include <stdio.h>
#include "SerialSocket.hpp"
#include <string.h>
#include <ctype.h>

class SSPortable : public SerialSocketPortable {
public:
    /* 串口移植相关. */
    virtual bool open(){
        return true;
    }

    virtual bool close(){
        return true;
    }

    virtual unsigned int read(void *buffer,unsigned int size){
        return 0;
    }

    virtual unsigned int write(void *buffer,unsigned int size){
        char *tx_buffer_ptr = (char*)buffer;
        printf("------------\r\n");
        for ( int n = 0; n < size; n++ ){
            if ( isprint(tx_buffer_ptr[n]) ){
                printf("0x%02X(%c) ",(unsigned char)tx_buffer_ptr[n],tx_buffer_ptr[n]);
            }else{
                printf("0x%02X ",(unsigned char)tx_buffer_ptr[n]);
            }
            if ( (n + 1) % 8 == 0 ){
                printf("\r\n");
            }
        }
        printf("\r\n");

        return size;
    }

    /* 操作系统移植相关. */
    virtual int getSemaphore(){
        return 0;
    }

    virtual bool freeSemaphore(int sem_number){
        return true;
    }

    virtual bool takeSemaphore(int sem_number){
        return true;
    }

    virtual bool releaseSemaphore(int sem_number){
        return true;
    }
};

int main(){
    char *message = "Hello";
    SSPortable ss_portable;
    SerialSockets *ss = SerialSockets::getInstance(&ss_portable);
    SerialSocket *socket = ss->get(10);
    if ( socket != nullptr ){
        int len = strlen(message);
        printf("Len(%d)\r\n",len);
        socket->write(message,len);
    }
    return 0;
}